import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '@/components/Home'


Vue.use(VueRouter)

const routes =  [
  {
    path:'/', 
    redirect: '/login'
  },
  { 
    path: '/login', 
    component: Login 
  },
  { 
    path: '/home', 
    component:Home,
    // 进入到home页面路由重定项
    redirect:'/welcome',
    children:[
      {path:'/welcome',component :()=>import('../components/Welcome.vue')},
      {path:'/users', component :()=>import('../components/user/Users.vue')},
      {path:'/rights', component :()=>import('../components/rights/Rights.vue')},
      {path:'/roles', component :()=>import('../components/Roles/Roles.vue')},
      {path:'/categories', component :()=>import('../components/goods/Cate.vue')},
      {path:'/params', component :()=>import('../components/goods/Params.vue')},
      
    ]
  }
]

const router = new VueRouter({
  routes
})
// 挂载路由导航守卫

router.beforeEach((to,from,next) =>{
  // to 将要访问的对象
  // from 代表从哪个路径来
  // next 是一个函数表示放行
  if(to.path ==='/login') return next();
  const tokenStr = window.sessionStorage.getItem('token');
  if(!tokenStr) return next('/login');
  next();
})

export default router
